package com.minelx.woods.server.perspective;

import com.minelx.woods.server.common.Parameters;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class JoinPointProcedure implements IProcedure {
	private final ProceedingJoinPoint proceedingJoinPoint;

	public JoinPointProcedure(ProceedingJoinPoint proceedingJoinPoint) {
		this.proceedingJoinPoint = proceedingJoinPoint;
	}

	@Override
	public boolean annotatedBy(Class<? extends Annotation> annotationClass) {
		MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
		return methodSignature.getMethod().isAnnotationPresent(annotationClass);
	}

	@Override
	public Parameters parameters() {
		MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
		return new Parameters(methodSignature.getMethod().getParameters(), proceedingJoinPoint.getArgs());
	}

	@Override
	public Object proceed() {
		try {
			return proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
		} catch (Throwable e) {
			throw new RuntimeException("something is wrong here", e);
		}
	}
}
